package dao

import (
	"errors"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/models"
	"order_srv/pkg"
)

// 改派创建
func ReassignCreate(in *__.ReassignCreateReq) (*models.Reassign, error) {
	var reassign models.Reassign
	var order models.Orders
	err := reassign.GetReassignInfo(int(in.OrderId))
	if err != nil {
		return nil, errors.New("查询改派失败" + err.Error())
	}
	if reassign.Id != 0 {
		return nil, errors.New("该订单已被改派")
	}
	err = order.GetOrderInfo(int(in.OrderId))
	if err != nil {
		return nil, errors.New("查询订单失败" + err.Error())
	}
	if order.Id == 0 {
		return nil, errors.New("该订单不存在")
	}
	if order.OrderStatus != 1 && order.OrderStatus != 2 && order.OrderStatus != 3 {
		return nil, errors.New("该订单不是待接单、待上车、行驶中状态，不能改派")
	}

	if order.DriverId == uint32(in.AssignDriver) {
		return nil, errors.New("新司机不能和当前司机相同")
	}
	// 敏感词
	if !pkg.Baid(in.Reason) {
		return nil, errors.New("改派原因包含敏感词")
	}

	reassign.OrderId = in.OrderId                  //订单id
	reassign.UserId = in.UserId                    //操作人用户id
	reassign.TakingDriver = uint64(order.DriverId) //当前司机
	reassign.AssignDriver = in.AssignDriver        //新司机
	reassign.Reason = in.Reason                    //改派原因
	err = reassign.ReassignCreate()
	if err != nil {
		return nil, err
	}
	return &reassign, nil
}
